import axios from 'axios';
import { API_BASE_URL } from '../config'; // 引入配置文件

const instance = axios.create({
  baseURL: API_BASE_URL,  // 使用配置文件中的基础路径
  timeout: 5000,
});

instance.interceptors.request.use((config) => {
  const token = localStorage.getItem("token");
  if (token) {
    config.headers.Authorization = `Bearer ${token}`;
  }
  console.log("Request Config:", config);
  return config;
});

instance.interceptors.response.use(
  (response) => {
    console.log("API Response:", response);
    return response;
  },
  (error) => {
    console.error("API Error:", error.response?.data || error.message);
    return Promise.reject(error);
  }
);

export default instance;
